package com.hbnu.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hbnu.system.config.BaseException;
import com.hbnu.system.core.util.PageUtil;
import com.hbnu.system.mapper.BriefingMapper;
import com.hbnu.system.model.dto.SearchPageDTO;
import com.hbnu.system.model.entity.Briefing;
import com.hbnu.system.service.IBriefingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2023-05-16
 */
@Service
public class BriefingServiceImpl extends ServiceImpl<BriefingMapper, Briefing> implements IBriefingService {

    @Autowired private IBriefingService iBriefingService;

    @Override
    public boolean addBriefing(Briefing briefing) {
        if(StrUtil.isEmpty(briefing.getName()) || StrUtil.isBlank(briefing.getName())){
            throw new BaseException("请输入名称");
        }

        Briefing one = super.getOne(Wrappers.lambdaQuery(Briefing.class)
                .eq(Briefing::getName, briefing.getName()));
        if(Objects.nonNull(one)){
            throw new BaseException("该名称已被使用!");
        }

        if(StrUtil.isEmpty(briefing.getSystemID()) || StrUtil.isBlank(briefing.getSystemID())){
            throw new BaseException("请输入系统编号");
        }

//        if(StrUtil.isEmpty(knowLeage.getAnnex().toString()) || StrUtil.isBlank(knowLeage.getAnnex().toString())){
//            throw new BaseException("请选择附件");
//        }

        LocalDate nowDate = LocalDate.now();

        if(StrUtil.isEmpty(briefing.getYear()) || StrUtil.isBlank(briefing.getYear())){
            throw new BaseException("请输入所属年度");
        }
        String substring = nowDate.toString().substring(0, 4);
        if(Integer.parseInt(briefing.getYear()) > Integer.parseInt(substring)){
            throw new BaseException("所属年度有误!");
        }
        if(Objects.isNull(briefing.getEnteredDate())) {
            briefing.setEnteredDate(LocalDate.now());
        }
        return baseMapper.insert(briefing) == 1;
    }

    @Override
    public boolean deleteBriefing(String briefingID) {
        Briefing briefing = super.getOne(Wrappers.lambdaQuery(Briefing.class)
                .eq(Briefing::getBriefingID, briefingID));
        if(Objects.isNull(briefing)){
            throw new BaseException("科研简报编号有误");
        }
        return baseMapper.deleteById(briefing) == 1;
    }

    @Override
    public boolean updateBriefing(Briefing briefing) {
        Briefing one = super.getOne(Wrappers.lambdaQuery(Briefing.class)
                .eq(Briefing::getBriefingID, briefing.getBriefingID()));
        if (Objects.isNull(one)){
            throw new BaseException("科研简报编号有误");
        }

        if(StrUtil.isEmpty(briefing.getName()) || StrUtil.isBlank(briefing.getName())){
            throw new BaseException("名称不可为空");
        }

        if(StrUtil.isEmpty(briefing.getSystemID()) || StrUtil.isBlank(briefing.getSystemID())){
            throw new BaseException("系统编号不可为空");
        }

//        if(StrUtil.isEmpty(knowLeage.getAnnex().toString()) || StrUtil.isBlank(knowLeage.getAnnex().toString())){
//            throw new BaseException("附件不可为空");
//        }

        LocalDate nowDate = LocalDate.now();

        if(StrUtil.isEmpty(briefing.getYear()) || StrUtil.isBlank(briefing.getYear())){
            throw new BaseException("所属年度不可为空");
        }
        String substring = nowDate.toString().substring(0, 4);
        if(Integer.parseInt(briefing.getYear()) > Integer.parseInt(substring)){
            throw new BaseException("所属年度有误!");
        }
//        briefing.setEnteredDate(nowDate);

        return baseMapper.updateById(briefing) == 1;
    }

    @Override
    public Briefing getBriefing(String briefingID) {
        Briefing briefing = super.getOne(Wrappers.lambdaQuery(Briefing.class)
                .eq(Briefing::getBriefingID, briefingID));
        if(Objects.isNull(briefing)){
            throw new BaseException("科研简报编号异常!未查找到该科研简报");
        }
        return briefing;
    }

    @Override
    public IPage<Briefing> pageCondition(SearchPageDTO searchPageDTO) {
        IPage<Briefing> page = PageUtil.getPage(searchPageDTO.getPageNum(), searchPageDTO.getPageSize());
        LambdaQueryWrapper<Briefing> wrapper = Wrappers.lambdaQuery(Briefing.class);
        // 检查 projectName 是否为空或空白
        if (StrUtil.isNotBlank(searchPageDTO.getSearch())) {
            // 如果 projectName 不为空或空白，添加模糊查询条件
            wrapper.like(Briefing::getName, searchPageDTO.getSearch());
        }
        IPage<Briefing> briefingIPage = baseMapper.selectPage(page, wrapper);

        List<Briefing> records = briefingIPage.getRecords();
        List<Briefing> BriefingS = new ArrayList<>();

        int count = 1;
        int start = searchPageDTO.getPageSize() * (searchPageDTO.getPageNum()-1)+1;
        int end = searchPageDTO.getPageNum() * searchPageDTO.getPageSize();
        Collections.reverse(records);
        for (Briefing record:records) {
            if (count >= start && count <= end){
                BriefingS.add(this.getBriefing(record.getBriefingID()));
            }
            count += 1;
        }

//        Collections.reverse(BriefingS);
        briefingIPage.setRecords(BriefingS);
        briefingIPage.setTotal(records.size());
        return briefingIPage;
    }
}
